﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmThemMonAn : Form
    {
        private MonAnService maService = new MonAnService();
        private NguyenLieuService nlService = new NguyenLieuService();
        public int flag = 0;
        public MonAn ma;
        List<NguyenLieu> listChiTiet = new List<NguyenLieu>();
        List<NguyenLieu> listNL = new List<NguyenLieu>();

        public frmThemMonAn()
        {
            InitializeComponent();
        }

        private void HienThiCTMonAn(DataTable dbtab)
        {
            listChiTiet.Clear();
            if (dbtab.Rows.Count > 0)
            {
                for (int i = 0; i < dbtab.Rows.Count; i++)
                {
                    DataRow dbrow = dbtab.Rows[i];
                    ListViewItem liv = new ListViewItem();
                    liv.Text = dbrow["MaNL"].ToString();
                    liv.SubItems.Add(dbrow["TenNL"].ToString());
                    liv.SubItems.Add(dbrow["SoLuong"].ToString());
                    lvChiTiet.Items.Add(liv);
                    NguyenLieu nl = new NguyenLieu();
                    nl.MaNL = int.Parse(dbrow["MaNL"].ToString());
                    nl.TenNL = dbrow["TenNL"].ToString();
                    nl.Soluong = int.Parse(dbrow["SoLuong"].ToString());
                    listChiTiet.Add(nl);
                }
            }
            else
            {
                MessageBox.Show("Lỗi");
                return;
            }
        }

        private void HienThiMonAn()
        {

            txtTen.Text = ma.Tenmon;
            txtDonGia.Text = ma.Dongia.ToString();
            cb_loaimonan.Text = maService.LayTenLoaiTheoMa(ma.Maloai);
            DataTable dbtab = maService.LayCTMonAn(ma.Mamonan);
            HienThiCTMonAn(dbtab);

        }

        private void LoadLoaiMonAn()
        {
            DataTable dbtab = maService.LoadLoaiMonAn();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                cb_loaimonan.Items.Add(dbrow["TenLoaiMonAn"]);
            }
            cb_loaimonan.Text = cb_loaimonan.Items[0].ToString();
        }

        private void LoadNguyeLieu()
        {
            DataTable dbtab = nlService.LayNguyenLieu();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                NguyenLieu nl = new NguyenLieu();
                nl.MaNL = int.Parse(dbrow["MaNL"].ToString());
                nl.TenNL = dbrow["TenNL"].ToString();
                listNL.Add(nl);
            }
            LoadNL();
        }

        private void frmThemMonAn_Load(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                LoadLoaiMonAn();
                LoadNguyeLieu();
            }
            else
            {
                HienThiMonAn();
                LoadNguyeLieu();
                XoaNLDaTonTaiTrongChiTiet();
                LoadLoaiMonAn();
                LoadNL();
            }
        }

        private void XoaNLDaTonTaiTrongChiTiet()
        {
            for (int i = 0; i < listNL.Count; i++)
                for (int j = 0; j < listChiTiet.Count; j++)
                    if (listChiTiet[j].MaNL == listNL[i].MaNL)
                        listNL.RemoveAt(i);
        }

        private void LoadNL()
        {
            lvNguyenLieu.Items.Clear();
            for (int i = 0; i < listNL.Count; i++)
            {
                ListViewItem liv = new ListViewItem();
                liv.Text = listNL[i].MaNL.ToString();
                liv.SubItems.Add(listNL[i].TenNL);
                lvNguyenLieu.Items.Add(liv);
            }
        }

        private void LoadChiTiet()
        {
            lvChiTiet.Items.Clear();
            for (int i = 0; i < listChiTiet.Count; i++)
            {
                ListViewItem liv = new ListViewItem();
                liv.Text = listChiTiet[i].MaNL.ToString();
                liv.SubItems.Add(listChiTiet[i].TenNL);
                liv.SubItems.Add(int.Parse(txtSoLuong.Text).ToString());
                lvChiTiet.Items.Add(liv);
            }
        }

        private void XoaNLTrongList(NguyenLieu nl)
        {
            for (int i = 0; i < listNL.Count; i++)
                if (listNL[i].MaNL == nl.MaNL)
                    listNL.RemoveAt(i);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            int index = lvNguyenLieu.FocusedItem.Index;
            if (index >= 0)
            {
                if (txtSoLuong.Text != "")
                {
                    NguyenLieu nl = new NguyenLieu();
                    nl.MaNL = int.Parse(lvNguyenLieu.Items[index].Text);
                    nl.TenNL = lvNguyenLieu.Items[index].SubItems[1].Text;
                    nl.Soluong = int.Parse(txtSoLuong.Text);
                    listChiTiet.Add(nl);
                    XoaNLTrongList(nl);
                    LoadChiTiet();
                    LoadNL();
                }
                else
                {
                    MessageBox.Show("Chưa điền số lượng nguyên liệu");
                    return;
                }
            }
            else
            {
                return;
            }
        }

        private void XoaNLTrongChiTiet(NguyenLieu nl)
        {
            for (int i = 0; i < listChiTiet.Count; i++)
                if (listChiTiet[i].MaNL == nl.MaNL)
                    listChiTiet.RemoveAt(i);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int index = lvChiTiet.FocusedItem.Index;
            if (index >= 0)
            {
                NguyenLieu nl = new NguyenLieu();
                nl.MaNL = int.Parse(lvChiTiet.Items[index].Text);
                nl.TenNL = lvChiTiet.Items[index].SubItems[1].Text;
                listNL.Add(nl);
                XoaNLTrongChiTiet(nl);
                LoadChiTiet();
                LoadNL();
            }
            else
            {
                return;
            }
        }

        private void btn_them_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                MonAn ma = new MonAn();
                ma.Tenmon = txtTen.Text;
                ma.Dongia = int.Parse(txtDonGia.Text);
                ma.Maloai = maService.LayMaLoaiTheoTen(cb_loaimonan.Text);
                if (maService.ThemMonAn(ma, listChiTiet))
                {
                    MessageBox.Show("Thành Công");
                    this.Close();
                    return;

                }
                else
                {
                    MessageBox.Show("Lỗi");
                    return;
                }
            }
            else
            {
                ma.Tenmon = txtTen.Text;
                ma.Dongia = int.Parse(txtDonGia.Text);
                ma.Maloai = maService.LayMaLoaiTheoTen(cb_loaimonan.Text);
                if (maService.SuaMonAn(ma, listChiTiet))
                {
                    MessageBox.Show("Thành Công");
                    this.Close();
                    return;

                }
                else
                {
                    MessageBox.Show("Lỗi");
                    return;
                }
            }
        }
    }
}
